<?php

class Image {

    public function __input() {
        return $this;
    }

    # 验证码
    public function Verify( $width = 110, $height = 35, $imageType = 'png' ) {
        header( "Content-type: image/" . $imageType );

        $c = array( "-", '*', '+' );
        $a = mt_rand( 2, 9 );
        $b = mt_rand( 2, 9 );
        $c = $c[ mt_rand( 0, 2 ) ];
        $d = "=";
        $e = "?";

        $randval = array( $a, $c, $b, $d, $e );

        $v = 0;
        eval( "\$v = $a $c $b;" );
        $_SESSION[ 'verify' ] = $v;

        $length = count( $randval );

        $im = imagecreate( $width, $height );
        imagefill( $im, 0, 0, imagecolorallocate( $im, 255, 255, 255 ) );
        $fontColor[ ] = imagecolorallocate( $im, 0x15, 0x15, 0x15 );
        $fontColor[ ] = imagecolorallocate( $im, 0x95, 0x1e, 0x04 );
        $fontColor[ ] = imagecolorallocate( $im, 0x93, 0x14, 0xa9 );
        $fontColor[ ] = imagecolorallocate( $im, 0x12, 0x81, 0x0a );
        $fontColor[ ] = imagecolorallocate( $im, 0x06, 0x3a, 0xd5 );

        // 画背景
        imagefilledrectangle( $im, 0, 0, $width, $height, imagecolorallocate( $im, 235, 236, 237 ) );
        // 画干扰线
        for ( $i = 0; $i < 20; $i++ ) {
            $font_color = imagecolorallocate( $im, mt_rand( 0, 255 ), mt_rand( 0, 255 ), mt_rand( 0, 255 ) );
            imagearc( $im, mt_rand( -$width, $width ), mt_rand( -$height, $height ), mt_rand( 30, $width * 2 ), mt_rand( 20, $height * 2 ), mt_rand( 0, 360 ), mt_rand( 0, 360 ), $font_color );
        }
        // 画干扰点
        for ( $i = 0; $i < 50; $i++ ) {
            $font_color = imagecolorallocate( $im, mt_rand( 0, 255 ), mt_rand( 0, 255 ), mt_rand( 0, 255 ) );
            imagesetpixel( $im, mt_rand( 0, $width ), mt_rand( 0, $height ), $font_color );
        }

        $font = APP_PATH . '/static/font/consola.ttf';
        $fontSize = 24;
        for ( $i = 0; $i < $length; $i++ ) {
            $x = floor( $width / $length ) * $i + 5;
            $y = $height / 2 + $fontSize / 2;
            imagettftext( $im, $fontSize, rand( 0, 5 ), $x, $y, $fontColor[ $i ], $font, $randval[ $i ] );
        }
        $this->output( $im, $imageType );
    }


    public function Thumb( $src_file, $thumbwidth, $thumbheight, $rel_type ) {

        $src_file = ltrim( $src_file, "\/\\" );
        // 原图绝对路径
        $source = APP_PATH . "/{$src_file}";

        // 原图不存在或不可读则退出
        if ( !is_readable( $source ) ) return $src_file;

        // 获取原图像宽高
        $result = getimagesize( $source );
        $imagewidth = $result[ 0 ];
        $imageheight = $result[ 1 ];

        // 如果来源图像小于或等于缩略图则返回源图像作为缩略图,免去操作
        if ( $imagewidth <= $thumbwidth && $thumbheight <= $thumbheight ) {
            return $src_file;
        }

        // 缩略图绝对路径
        $fileParts = pathinfo( $source );
        $thumb_path = "{$fileParts[ 'dirname' ]}/{$fileParts[ 'filename' ]}_{$thumbwidth}_{$thumbheight}.{$fileParts[ 'extension' ]}";

        // 缩略图相对路径
        $thumb_file = ltrim( str_replace( APP_PATH, "", $thumb_path ), "\/\\" );

        // 如果缩略图存在则按$rel_type返回
        if ( @is_readable( $thumb_file ) ) {
            return ( $rel_type == 1 ? $GLOBALS[ 'setting' ][ 'weburl' ] : APP_PATH ) . "/{$thumb_file}";
        }

        $type = strtolower( $fileParts[ 'extension' ] ) == 'bmp' ? 'jpg' : strtolower( $fileParts[ 'extension' ] );
        $type == 'jpg' && $type = 'jpeg';

        if ( $thumbwidth && !$thumbheight ) { //有宽无高
            $thumb[ 'width' ] = $thumbwidth;
            $thumb[ 'height' ] = $imageheight * ( $thumbwidth / $imagewidth );
        } else if ( !$thumbwidth && $thumbheight ) { //有高无宽
            $thumb[ 'width' ] = $imagewidth * ( $thumbheight / $imageheight );
            $thumb[ 'height' ] = $thumbheight;
        } else { // 计算缩放比例
            $scale = min( $thumbwidth / $imagewidth, $thumbheight / $imageheight );
            if ( $scale >= 1 ) { // 超过原图大小不再缩略
                $thumb[ 'height' ] = $imageheight;
                $thumb[ 'width' ] = $imagewidth;
            } else { // 缩略图尺寸
                $thumb[ 'height' ] = (int)( $imageheight * $scale );
                $thumb[ 'width' ] = (int)( $imagewidth * $scale );
            }
        }

        // 载入原图
        $createFun = "Imagemc_classreateFrom{$type}";
        $src_img = $createFun( $source );

        // 创建缩略图
        if ( $type != 'gif' && function_exists( 'imagecreatetruecolor' ) ) {
            $thumb_img = imagecreatetruecolor( $thumb[ 'width' ], $thumb[ 'height' ] );
        } else {
            $thumb_img = imagecreate( $thumb[ 'width' ], $thumb[ 'height' ] );
        }

        // 复制图片
        if ( function_exists( 'imagecopyresampled' ) ) {
            @Imagemc_classopyResampled( $thumb_img, $src_img, 0, 0, 0, 0, $thumb[ 'width' ], $thumb[ 'height' ], $imagewidth, $imageheight );
        } else {
            @Imagemc_classopyResized( $thumb_img, $src_img, 0, 0, 0, 0, $thumb[ 'width' ], $thumb[ 'height' ], $imagewidth, $imageheight );
        }

        //生成缩略图
        $outfunc = "image{$type}";
        if ( $type == 'jpeg' ) { // 对jpeg图形设置隔行扫描与图片质量
            imageinterlace( $thumb_img, 1 );
            $outfunc( $thumb_img, $thumb_path, 95 );
        } else {
            $outfunc( $thumb_img, $thumb_path );
        }

        //销毁临时图像
        @ImageDestroy( $src_img );
        @ImageDestroy( $thumb_img );

        // 返回缩略图
        return ( $rel_type == 1 ? $GLOBALS[ 'setting' ][ 'weburl' ] : APP_PATH ) . "/{$thumb_file}";
    }

    private function output( $im, $type ) {
        $ImageFun = 'image' . $type;
        $ImageFun( $im );
        imagedestroy( $im );
    }
}